/**
 * Created by sxwu on 14-2-15.
 */


var fields_all = "customerid,wifiid,custphone,custname,custregdatetime,custtotalflow,custtotaltime,custflag,custmac,custip";

/**
 * 模块依赖
 */
var db = require("../lib/db");

//查找记录
exports.select = function(userid, handler) {
    db.execQuery({
        "sql": "SELECT * FROM customerinfo WHERE userid=? order by custregdatetime desc",
        "args": [userid],
        "handler": handler
    });

};

//查询顾客是否已经通过该AP上线过
exports.selectCustExists = function(mac, handler) {
    db.execQuery({
        "sql": "SELECT * FROM customerinfo  WHERE custmac=? and apmac=?",
        "args": [
            mac.wlanusermac,
            mac.apmac
        ],
        "handler": handler
    });

};
exports.selectMac = function(mac, handler) {
    db.execQuery({
        "sql": "SELECT * FROM customerinfo  WHERE custmac=? order by custregdatetime desc",
        "args": [
            mac
        ],
        "handler": handler
    });

};

//插入记录
exports.insert = function(customerinfo, handler) {
    db.execQuery({
        "sql": "insert into customerinfo(wifiid,userid,custphone,custname,custregdatetime,custtotalflow,custtotaltime,custflag,custmac,custip,apmac) " +
            "values(?,?,?,?,?,?,?,?,?,?,?)",
        "args":[ customerinfo.wifiid,
            customerinfo.userid,
            customerinfo.custphone,
            customerinfo.custname,
            customerinfo.custregdatetime,
            customerinfo.custtotalflow,
            customerinfo.custtotaltime,
            customerinfo.custflag,
            customerinfo.custmac,
            customerinfo.custip,
            customerinfo.apmac],
        "handler": handler
    });
};
//updateCounters
exports.updateCounters= function(customerinfo, handler) {
    db.execQuery({
        "sql": "update customerinfo set custtotalflow=custtotalflow+?, custtotaltime=custtotaltime+? WHERE customerid=?",
        "args": [
            customerinfo.logcurrentflow,
            customerinfo.logcurrenttime,
            customerinfo.customerid],
        "handler": handler
    });
};

//online
exports.updateOnline= function(wherclause, handler) {
    db.execQuery({
        "sql": "update customerinfo set custflag=1 WHERE custmac=? and apmac=?",
        "args": [
            wherclause.wlanusermac,
            wherclause.apmac],
        "handler": handler
    });
};

//offline
exports.updateOffline= function(wherclause, handler) {
    db.execQuery({
        "sql": "update customerinfo set custflag=2 WHERE custmac=? and apmac=?",
        "args": [
            wherclause.wlanusermac,
            wherclause.apmac],
        "handler": handler
    });
};

//注销客户
exports.updateInactive= function(customerid, handler) {
    db.execQuery({
        "sql": "update customerinfo set custflag=0 WHERE customerid=?",
        "args": [customerid],
        "handler": handler
    });

};
//更新记录
exports.update = function(wifiinfo, handler) {
    db.execQuery({
        "sql": "update customerinfo set wifiid=?,custphone=?,custname=?,custregdatetime=?,custtotalflow=?,custtotaltime=?,custflag=?,custip=?,apmac=?" +
            " WHERE customerid=?",
        "args": [
            customerinfo.wifiid,
            customerinfo.custphone,
            customerinfo.custname,
            customerinfo.custregdatetime,
            customerinfo.custtotalflow,
            customerinfo.custtotaltime,
            customerinfo.custflag,
            customerinfo.custip,
            customerinfo.apmac,
            wifiinfo.customerid],
        "handler": handler
    });

};

//遍历记录
exports.findAll = function(handler) {
    db.execQuery({
        "sql": "SELECT * FROM customerinfo ORDER BY customerid asc",
        "handler": handler
    });
};
